<template>
  <div class="form-layout">
    <!--左侧导航-->
    <div class="nav-left">
      <customAnchor :itemList="[{ name: 'box-data', title: cpBusinessTypeTttle + '信息' },{ name: 'box-base', title: '资质详情' },  { name: 'box-img', title: '资质附件' }]" />
    </div>
    <!--表单区域-->
    <div class="form-right" ref="formRight"  style="min-height: 80vh">
      <customForm :data="form" :rules="propRules" ref="editForm" :disabled="propReadonly">
        <template #content>
          <div id="box-data" class="box-title">{{cpBusinessTypeTttle + '信息'}}</div>
          <!-- 供应商信息-->
          <template v-if="constCommon.certTypeNameMap.sp.businessTypeCode == form.businessTypeCode">
            <customFormItem label="名称" name="name" :parent="_self" :form="certInfo" disabled/>
            <customFormItem label="社会信用代码" name="companyCode" :parent="_self" :form="certInfo"  disabled/>
          </template>

          <!-- 厂商信息-->
          <template v-if="constCommon.certTypeNameMap.mfg.businessTypeCode == form.businessTypeCode">
            <customFormItem label="名称" name="name" :parent="_self" :form="certInfo"  disabled/>
            <customFormItem label="社会信用代码" name="companyCode" :parent="_self" :form="certInfo"  disabled/>
            <customFormItem label="生产许可证" name="productionLicence" :parent="_self" :form="certInfo"  disabled/>
          </template>

          <!-- 品类信息-->
          <template v-if="constCommon.certTypeNameMap.cat.businessTypeCode == form.businessTypeCode">
            <customFormItem label="名称" name="name" :parent="_self" :form="certInfo"  disabled/>
            <customFormItem label="代码" name="code" :parent="_self" :form="certInfo"  disabled/>
          </template>

          <!-- 商品信息-->
          <template v-if="constCommon.certTypeNameMap.matr.businessTypeCode == form.businessTypeCode">
            <customFormItem label="品名" name="materialName" :parent="_self" :form="certInfo"  disabled/>
            <customFormItem label="商品名" name="materialTradeName" :parent="_self" :form="certInfo"  disabled/>
            <customFormItem label="注册证" name="certificateNo" :parent="_self" :form="certInfo"  disabled/>
            <customFormItem label="注册证开始" name="certificateExpiredStartDate" :parent="_self" :form="certInfo"  disabled/>
            <customFormItem label="注册证截止" name="certificateExpiredDate" :parent="_self" :form="certInfo"  disabled/>
            <customFormItem label="批准文号" name="approvalNo" :parent="_self" :form="certInfo"  disabled/>
          </template>


          <div id="box-base" class="box-title">资质详情</div>
          <customFormItem
            label="资质类型" name="certificateName" :parent="_self" :form="form"
            @focusTo="$refs.certificateName.focus()"
            :type="constCommon.formItemType.slot.name"
          >
            <template #content>
              <certificateSelect
                v-model="form.certificateId"
                :valueDisplay="form.certificateName"
                @change="tableDrawerTypeConfirm"
                ref="certificateName"
                :disabled="!cpNew"
                :queryBusinessTypeCode="form.businessTypeCode"
              />
            </template>
          </customFormItem>
          <customFormItem label="版本" name="version" :parent="_self" :form="form" disabled
                          helpTip="初始化为1，后续每【提交升级】一次，版本号+1"
          />

          <customFormItem label="签发方" name="certificateSign" :parent="_self" :form="form"
                          helpTip="1、签发机构名称，如XX 省市场监督管理局<br>2、上级代理单位，如xx省医疗器械有限公司"
                          full-width
          />
          <customFormItem label="授予方" name="certificateSignTo" :parent="_self" :form="form"
                          helpTip="授予公司名称，如xx市医疗器械有限公司"
                          full-width
          />
          <customFormItem label="资质编号" name="certificateNo" :parent="_self" :form="form"
                          v-show="1 == form.needCertificateNo"
                          helpTip="1、营业执照：填写统一信用代码<br>2、注册证:填写注册证号<br>3、其他类型:填写唯一编码即可"
          />
          <customFormItem label="效期开始" name="expiryStartDate" :parent="_self" :form="form"
            :type="constCommon.formItemType.datePicker.name"
                          v-show="1 == form.needExpiryDate"
                          helpTip="效期的开始日期"
          />
          <customFormItem label="效期结束" name="expiryDate" :parent="_self" :form="form"
            :type="constCommon.formItemType.datePicker.name"
                          v-show="1 == form.needExpiryDate"
                          helpTip="效期的截止日期，如果未长期，则选择2099-01-01即可"
          />

          <customFormItem label="备注" name="remark" :parent="_self" :form="form"
                :type="constCommon.formItemType.textarea.name"
                fullWidth
                :config="{maxlength: 200, minRows: 1, maxRows: 5}"
          />
          <div id="box-img" class="box-title">资质附件</div>
          <customFormItem
            label=""
            name="certFile"
            :parent="_self"
            :form="form"
            :type="constCommon.formItemType.slot.name"
            fullWidth
            style="height: 200px; padding-left: 10px;"
          >
            <template #content>
              <customUpload
                :data="{ fileRefId: form.id, fileRefTable: 'certificate_repository', fileSavePath: 'certificate_repository',fileInfoIdArr: form.docIds,fileInfoField: 'doc_ids'}"
                hideMsg="请先保存后再上传"
                :disabled="propReadonly"
              />
            </template>
          </customFormItem>
        </template>
      </customForm>
    </div>
  </div>
</template>

<script>
import customAnchor from '@/components/custom/form/customAnchor.vue'
import customUpload from '@/components/custom/form/customUpload.vue'
import certificateSelect from '@/components/custom/bus/select/certificateSelect.vue'

export default {
  help: '资质-详情',
  data() {
    return {
      form: {},
      certInfo: {},
      viewData: {},
    }
  },
  methods: {
    // 资质类型选择后
    tableDrawerTypeConfirm(row) {
      this.$set(this.form, 'certificateId', row.id)
      this.$set(this.form, 'certificateName', row.name)
      this.$set(this.form, 'needExpiryDate', row.needExpiryDate)
      this.$set(this.form, 'needCertificateNo', row.needCertificateNo)
    },
    // 新增初始化
    initNew() {
      if (!this.propParent.cpFormNew) {
        return
      }
      this.$set(this.form, 'version', 1)
      this.$set(this.form, 'closeFlag', 0)
      this.$set(this.form, 'submitFlag', 0)
      this.$set(this.form, 'targetDescribeId', this.propInitData.targetDescribeId)
      this.$set(this.form, 'supplierId', this.propInitData.spId)
      this.$set(this.form, 'supplierName', this.propInitData.spName)
      this.$set(this.form, 'businessTypeCode', this.constCommon.certTypeNameMap[this.propInitData.typeName].businessTypeCode)
      let certExtName = this.propInitData.spName
      switch (this.propInitData.typeName) {
        // 供应商资质
        case this.constCommon.certTypeNameMap.sp.key: {
          break
        }
        // 厂商资质
        case this.constCommon.certTypeNameMap.mfg.key: {
          this.$set(this.form, 'manufacturerId', this.propInitData.mfgId)
          this.$set(this.form, 'manufacturerName', this.propInitData.mfgName)
          certExtName = this.propInitData.mfgName
          break
        }
        // 分类资质
        case this.constCommon.certTypeNameMap.cat.key: {
          this.$set(this.form, 'manufacturerId', this.propInitData.mfgId)
          this.$set(this.form, 'manufacturerName', this.propInitData.mfgName)
          this.$set(this.form, 'catalogId', this.propInitData.catId)
          this.$set(this.form, 'catalogName', this.propInitData.catName)
          certExtName = this.propInitData.catName
          break
        }
        // 商品资质
        case this.constCommon.certTypeNameMap.matr.key: {
          this.$set(this.form, 'manufacturerId', this.propInitData.mfgId)
          this.$set(this.form, 'manufacturerName', this.propInitData.mfgName)
          this.$set(this.form, 'catalogId', this.propInitData.catId)
          this.$set(this.form, 'catalogName', this.propInitData.catName)
          this.$set(this.form, 'materialName', this.propInitData.matrName)
          this.$set(this.form, 'materialId', this.propInitData.matrId)
          certExtName = this.propInitData.matrName
          break
        }
      }
      this.$set(this.form, 'certExtName', certExtName)
      let idTmp = 'temp_' + this.utils.uuid()
      this.$set(this.form, 'id', idTmp)
      this.propParent.$set(this.propParent.viewFormData, 'flowStatus', 'start')
      this.propParent.$set(this.propParent.viewFormData, 'id', idTmp)
      this.propParent.$set(this.propParent, 'viewFormStatus', 'edit')
      this.$nextTick(() => {
        setTimeout(() => {
          this.$refs.certificateName.focus()
        }, 500)
      })
    },

    // 资质信息
    getCertInfo() {
      this.certInfo = {}
      let config = Object.values(this.constCommon.certTypeNameMap).find(tmp => tmp.businessTypeCode == this.form.businessTypeCode)
      if (config) {
        let getAddr = `${config.findById}?id=${this.form[config.id]}`
        this.$ajax.get(getAddr).then((response) => {
          if (response.code == 0) {
            this.certInfo = response.data
          }
        })
      }

    }
  },
  created() {
    let that = this
    this.$emit('event-register', {
      validate: (callback) => {
        return this.$refs.editForm.validate(callback)
      },
      beforeSave: () => {
        if (1 != this.form.needCertificateNo) {
          this.$set(this.form, 'certificateNo', null)
        }
        if (1 != this.form.needExpiryDate) {
          this.$set(this.form, 'expiryStartDate', null)
          this.$set(this.form, 'expiryDate', null)
        }
        this.$emit('data-callback', this.form)
        return true
      },
      afterMove: (formData) => {
        this.form = global.utils.jsonClone(formData)
        this.viewData = this.propCurrentRow
        try {
          this.initNew()
        }catch (e) {
          console.error('初始化失败', e)
        }
        this.getCertInfo()
      },
      afterSave: () => {
      },
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {
      },
      beforeFlow: (flowButtonKey) => {
        return true
      },
      afterFlow: (flowButtonKey) => {
      },
      beforeClose: () => {
        return true
      },
      afterClick(button, data) {
        let {key: buttonKey} = button
        // 提交升级
        if (buttonKey == 'upgrade') {
          that.utilMessage.confirm(button.actionTitle, () => {
            that.$set(that.form, 'opType', 'upgrade')
            that.propParent.ocAsynSaveViewForm()
          })
        }
        // 提交
        if (buttonKey == 'submit') {
          that.utilMessage.confirm(button.actionTitle, () => {
            that.$set(that.form, 'opType', 'submit')
            that.$set(that.form, 'submitFlag', 1)
            that.propParent.ocAsynSaveViewForm()
          })
        }
      },
    })
  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propStatus: String,
    propRules: Object,
  },
  components: { customAnchor, customUpload, certificateSelect },
  computed: {
    // 资质类型名称
    cpBusinessTypeTttle() {
      let title = ''
      Object.keys(this.constCommon.certTypeNameMap).forEach(key => {
        if (this.constCommon.certTypeNameMap[key].businessTypeCode == this.form.businessTypeCode) {
          title = this.constCommon.certTypeNameMap[key].title
        }
      })
      return title
    },
    cpNew() {
      return !this.form.id  ||  this.form.id.indexOf('temp_') != -1
    }
  },
}
</script>
